package com.example.mybatisannotation.service.impl;

import com.example.mybatisannotation.entity.Student;
import com.example.mybatisannotation.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int create(Student student){
        String strSql="insert in student(no,name,sex,phone,qq,id) value(?,?,?,?,?,?)";
        return jdbcTemplate.update(strSql, student.getNo(),student.getName(),student.getSex(),student.getPhone(),student.getQq(),student.getId());
    }
    @Override
    public List<Student> getByName(String name){
        String strSql="select * from student where name like CONCAT('%',?'%')";
        return jdbcTemplate.query(strSql,new BeanPropertyRowMapper<>(Student.class),name);
    }
    @Override
    public Student getByID(String id){
        String strSql="select * from student where id=?";
        Student stu=jdbcTemplate.queryForObject(strSql,new BeanPropertyRowMapper<>(Student.class),new Object[]{id});
        return stu;
    }
    @Override
    public int deleteById(String id){
        String strSql="delete from student where id=?";
        return jdbcTemplate.update(strSql,id);
    }
    @Override
    public List<Student> getAllStudents(){
        String strSql="select * from student";
        return jdbcTemplate.query(strSql,new BeanPropertyRowMapper<>(Student.class));
    }
    @Override
    public int updateStu(Student stu){
        String strSql="update student set no=?,name=?,sex=?"+"phone=?,qq=? where id=?";
        Object [] argus={stu.getNo(),stu.getName(),stu.getSex(),stu.getPhone(),stu.getQq(),stu.getId()};
        return jdbcTemplate.update(strSql,argus);
    }
}
